package com.hh.controller;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Administrator on 15-11-14.
 */
@Controller
@RequestMapping("/")
public class SendMsgController {

	@Autowired
	private JmsTemplate jmsTemplate;
	@Autowired
	@Qualifier("queueDestination") // 在配置文件中定义了两个queueDestination。这里确实能够使用其中的一个
	private Destination destination;

	/**
	 * 向指定队列发送消息
	 */
	@RequestMapping("sendMessage")
	public void sendMessage(Destination destination, final String msg) {
		System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg);
		jmsTemplate.send(destination, new MessageCreator() {

			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(msg);
			}
		});
	}

	/**
	 * 向默认队列发送消息
	 */
	public void sendMessage(final String msg) {
		String destination = jmsTemplate.getDefaultDestination().toString();
		System.out.println("向队列" + destination + "发送了消息------------" + msg);
		jmsTemplate.send(new MessageCreator() {

			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(msg);
			}
		});

	}
}
